package com.thinkwide.data.ormapping;

import com.thinkwide.data.core.MFString;
import com.thinkwide.data.exchange.MFExchange;
import com.thinkwide.data.ormapping.datatable.MFDataColumn;
import com.thinkwide.data.ormapping.datatable.MFDataColumnException;
import com.thinkwide.data.ormapping.datatable.MFDataTable;
import org.apache.commons.collections4.map.LinkedMap;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * 数据库结构管理
 *
 * @author andyzhang
 * @since 2010-12-29
 */
public class MFDbStruct {


    public static MFDataTable getTableFields(String tableName) throws SQLException, MFDataColumnException {
        return getTableFields(null, tableName);
    }

    /**
     * 根据表名获取表结构
     *
     * @param adapter
     * @param tableName
     * @return
     * @throws SQLException
     * @throws MFDataColumnException
     */
    public static MFDataTable getTableFields(MFDataAdapter adapter, String tableName) throws SQLException, MFDataColumnException {
        if (adapter == null) {
            adapter = MFORMappingGlobe.getDataAdapter();
        }
        MFDataTable table = adapter.getDataTable(String.format("select * from %s where 1=2", tableName));
        table.setTableName(tableName);
        return table;
    }

    public static MFDataTable getUpdateTable(String tableName) throws SQLException, MFDataColumnException {
        return getUpdateTable(null, tableName);
    }

    public static MFDataTable getUpdateTable(MFDataAdapter adapter, String tableName) throws SQLException, MFDataColumnException {
        if (adapter == null) {
            adapter = MFORMappingGlobe.getDataAdapter();
        }
        MFDataTable table = adapter.getDataTable(String.format("select * from %s where 1=2", tableName));
        table.setTableName(tableName);
        table.setModifyFields(getFieldsString(table));
        table.setPrimaryFields(MFExchange.getListToText(adapter.getPrimaryKeys(tableName), ","));
        return table;
    }

    public static MFDataTable createFields(String tableName) throws SQLException, MFDataColumnException {
        return createFields(null, tableName);
    }

    /**
     * 根据表名获取表结构,并且获取update字段
     *
     * @param adapter
     * @param tableName
     * @return
     * @throws SQLException
     * @throws MFDataColumnException
     */
    public static MFDataTable createFields(MFDataAdapter adapter, String tableName) throws SQLException, MFDataColumnException {
        if (adapter == null) {
            adapter = MFORMappingGlobe.getDataAdapter();
        }
        MFDataTable table = adapter.getDataTable(String.format("select * from %s where 1=2", tableName));
        table.setTableName(tableName);
        table.setModifyFields(getFieldsString(table));

        return table;
    }

    public static MFDataTable createFields(MFDataAdapter adapter, String tableName, String primaryFields) throws SQLException, MFDataColumnException {
        if (adapter == null) {
            adapter = MFORMappingGlobe.getDataAdapter();
        }
        MFDataTable table = adapter.getDataTable(String.format("select * from %s where 1=2", tableName));
        table.setTableName(tableName);
        table.setPrimaryFields(primaryFields);
        table.setModifyFields(getFieldsString(table));

        return table;
    }

    public static String getFieldsString(MFDataTable table) throws SQLException, MFDataColumnException {
        String updateFields = "";
        for (MFDataColumn col : table.getColumns()) {
            if (MFString.notNullOrEmpty(updateFields)) {
                updateFields = updateFields + "," + col.getField();
            } else {
                updateFields = updateFields + col.getField();
            }
        }
        return updateFields;
    }

    public static void prepareFieldsStringFix(MFDataAdapter adapter, MFDataTable table, String tableName, String primaryFields) throws SQLException, MFDataColumnException {
        if (adapter == null) {
            adapter = MFORMappingGlobe.getDataAdapter();
        }
        table.setTableAndPrimary(tableName, primaryFields);
        List<String> list = getTableFieldList(adapter, tableName);
        table.setInsertFields(getInsertFieldsStringFix(list));
        table.setModifyFields(getModifyFieldsStringFix(list));
    }

    public static void prepareFieldsStringFix(MFDataTable table, String tableName, String primaryFields) throws SQLException, MFDataColumnException {
        prepareFieldsStringFix(null, table, tableName, primaryFields);
    }

    public static String getModifyFieldsStringFix(String tableName) throws SQLException, MFDataColumnException {
        return getModifyFieldsStringFix(null, tableName);
    }

    public static String getModifyFieldsStringFix(List<String> list) throws SQLException, MFDataColumnException {
        String str = "";
        for (String item : list) {
            if (!MFString.compareNoCase("dCreateDate", item) && !MFString.compareNoCase("sCreateUser", item)) {
                if (MFString.isNullOrEmpty(str)) {
                    str = item;
                } else {
                    str = str + "," + item;
                }
            }

        }
        return str;
    }

    public static String getModifyFieldsStringFix(MFDataAdapter adapter, String tableName) throws SQLException, MFDataColumnException {
        if (adapter == null) {
            adapter = MFORMappingGlobe.getDataAdapter();
        }

        List<String> list = getTableFieldList(adapter, tableName);
        String str = getModifyFieldsStringFix(list);

        return str;
    }

    public static String getInsertFieldsStringFix(String tableName) throws SQLException, MFDataColumnException {
        return getInsertFieldsStringFix(null, tableName);
    }

    public static String getInsertFieldsStringFix(List<String> list) throws SQLException, MFDataColumnException {
        String str = "";
        for (String item : list) {
            if (!MFString.compareNoCase("dModifyDate", item) && !MFString.compareNoCase("sModifyUser", item)) {
                if (MFString.isNullOrEmpty(str)) {
                    str = item;
                } else {
                    str = str + "," + item;
                }
            }

        }
        return str;
    }

    public static String getInsertFieldsStringFix(MFDataAdapter adapter, String tableName) throws SQLException, MFDataColumnException {
        if (adapter == null) {
            adapter = MFORMappingGlobe.getDataAdapter();
        }

        List<String> list = getTableFieldList(adapter, tableName);
        String str = getInsertFieldsStringFix(list);
        return str;
    }

    public static List<String> getTableFieldList(MFDataAdapter adapter, String tableName) throws SQLException, MFDataColumnException {
        MFDataTable table = getTableFields(adapter, tableName);
        List<String> list = new ArrayList<String>();

        for (MFDataColumn col : table.getColumns()) {
            list.add(col.getField());
        }
        return list;
    }

    public static Map<String, String> getTableFieldMap(MFDataAdapter adapter, String tableName) throws SQLException, MFDataColumnException {
        MFDataTable table = getTableFields(adapter, tableName);
        Map<String, String> map = new LinkedMap();

        for (MFDataColumn col : table.getColumns()) {
            map.put(col.getField(), col.getDataType());
        }
        return map;
    }
}
